<!-- 排班管理 -->
<template>
  <div class="dashboard-container transport-task customer-list-box">
    <el-card class="box-card">
      <el-form label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="员工账号">
              <el-input
                placeholder="请输入员工账号"
                v-model="dataForm.employeeNumber"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="员工姓名">
              <el-input
                placeholder="请输入员工姓名"
                v-model="dataForm.name"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工作模式">
              <el-select
                v-model="dataForm.workPatternId"
                placeholder="请选择工作模式"
              >
                <el-option
                  v-for="item in workmodelList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="月份">
              <!-- 月份 -->
              <el-date-picker
                v-model="dataForm.month"
                type="month"
                placeholder="选择月"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="所属机构"
              style="margin-bottom: 20px"
              label-width="110px"
            >
              <treeselect
                v-model="dataForm.agencyId"
                :options="agencyOptions"
                :normalizer="normalizer"
                :searchable="true"
                style="width: 100%"
                placeholder="请选择目的地机构"
                no-options-text="暂无数据"
                no-results-text="暂无数据"
                @select="selectEndAgency"
                @open="opens"
                @close="closes"
              />
              <img
                ref="arrows"
                src="@/assets/pullDown.png"
                style="
                      position: absolute;
                      width: 18px;
                      height: 18px;
                      right: 9px;
                      top: 12px;
                      cursor: pointer;
                      pointer-events: none;
                      transition-duration: 0.3s;
                    "
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item>
              <el-button type="danger" @click="search">搜索</el-button>
              <el-button @click="reset" type="danger" plain>重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="box-card">
      <el-button type="danger" @click="$router.push('workArrange-setting')"
        >排班设置</el-button
      >
      <el-button type="danger" plain @click="okbtn">绑定排班</el-button>
      <el-table
        :data="tableData"
        style="width: 100%;margin:30px 0"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column
          fixed
          prop="employeeNumber"
          label="员工账号"
          width="120"
        >
        </el-table-column>
        <el-table-column fixed prop="name" label="员工姓名" width="120">
        </el-table-column>
        <el-table-column fixed prop="phone" label="员工电话" width="120">
        </el-table-column>
        <el-table-column fixed prop="userType" label="员工角色" width="100">
          <template #default="{row}">
            <span>{{
              row.userType === 1
                ? '员工'
                : row.userType === 2
                ? '快递员'
                : '司机'
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          fixed
          prop="workPatternName"
          label="当前工作模式"
          min-width="120"
          fit
        >
        </el-table-column>
        <el-table-column
          prop="size"
          :label="(index + 1).toString()"
          v-for="(item, index) in workSchedules.length"
          :key="index"
        >
          <template #default="{row}">
            <div v-if="row.workSchedules[index]" class="work">上</div>
            <div v-else class="rest">休</div>
          </template>
        </el-table-column>
        <template #empty>
          <el-empty description="空空如也"></el-empty>
        </template>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="+page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="+pageSize"
        layout=" total,sizes,prev, pager, next, jumper"
        :total="total"
        background
        style="margin-bottom:30px;text-align:center"
      >
      </el-pagination>
    </el-card>
    <!-- 弹框 -->
    <el-dialog
      title="人工调整"
      :visible.sync="showDialog"
      width="566px"
      class="work-type"
      @close="cancle"
    >
      <el-form label-width="80px">
        <el-row type="flex" justify="center">
          <el-col :span="18">
            <el-form-item>
              <el-radio-group
                v-model="radio"
                style="margin-left:-30px;"
                @input="radioinput"
              >
                <el-radio :label="1" style="margin-right:60px;font-weight:700;"
                  >礼拜制</el-radio
                >
                <el-radio :label="2" style="font-weight:700;">连续制</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row type="flex" justify="center">
          <el-col :span="18">
            <el-form-item label="模式名称">
              <el-select
                v-model="workPatternId2"
                placeholder="请选择工作模式"
                style="width:293px"
                @change="workPatternChange"
              >
                <el-option
                  v-for="item in workmodelList2"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row
          type="flex"
          justify="center"
          class="dialog-footer"
          style="margin-top:20px;"
        >
          <el-col :span="18">
            <el-form-item>
              <el-button
                type="danger"
                style="margin-left:60px;background-color:#e15536;color:#fff"
                @click="determine"
                >确认</el-button
              >
              <el-button @click="cancle" type="danger" plain>取消</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { agencyList } from '@/api/institutions'
import { selectWorkHistoryList, workManage, peopleSet } from '@/api/transit'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import Treeselect from '@riophae/vue-treeselect'
export default {
  components: {
    Treeselect
  },
  data() {
    return {
      option: '',
      page: 1,
      pageSize: 10,
      total: 0,
      dataForm: {
        // 员工账号
        employeeNumber: '',
        // 员工姓名
        name: '',
        // 月份
        month: '',
        // 工作模式名称
        workPatternId: '',
        agencyId: null
      },
      workPatternId2: '',
      // 工作模式列表
      workmodelList: '',
      workmodelList2: '',
      agencyOptions: [],
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      },
      params: {
        agencyId: 0,
        employeeNumber: '',
        month: 'yy-mm',
        name: '',
        page: 0,
        pageSize: 10,
        userType: 1,
        workPatternId: 0
      },
      tableData: [],
      workSchedules: [],
      showDialog: false,
      radio: 1,
      userIdList: [],
      // 弹框中工作模式名称
      workPatternType: '',
      workPatternId: ''
    }
  },
  created() {
    this.selectWorkHistoryList()
    this.getAgencyList()
    this.workManage()
  },
  methods: {
    // 工作模式列表（下拉）
    async selectWorkHistoryList() {
      const res = await selectWorkHistoryList()
      // console.log(res.data[1])
      this.workmodelList = res.data[1].concat(res.data[2])
      this.workmodelList2 = JSON.parse(JSON.stringify(res.data[1]))
    },
    // 获取起始/目的地机构列表数据
    async getAgencyList() {
      const { data: res } = await agencyList()
      // 获取数据给options
      this.agencyOptions = JSON.parse(res)
    },
    selectEndAgency(val) {
      this.dataForm.agencyId = val.id
    },
    opens() {
      this.$refs.arrows.style.transform = 'rotate(-180deg)'
    },
    closes() {
      this.$refs.arrows.style.transform = 'rotate(0deg)'
    },
    // 排班管理列表
    async workManage(params) {
      const res = await workManage(params)
      console.log(res)
      this.tableData = res.data.items
      // console.log(this.tableData)
      this.total = +res.data.counts
      // console.log(res.data.items)
      this.workSchedules = []
      if (res.data.items.length) {
        this.workSchedules = res.data.items[0].workSchedules
      }
    },
    // 分页器
    handleSizeChange(val) {
      this.pageSize = +val
      this.workManage({ page: this.page, pageSize: this.pageSize })
    },
    async handleCurrentChange(page) {
      this.page = +page
      this.workManage({ page: this.page, pageSize: this.pageSize })
    },
    search() {
      // console.log(this.dataForm.month)
      // 搜索时候先清空初始值的天数数据，拿到重新渲染

      if (this.dataForm.month) {
        const date = new Date(this.dataForm.month)
        // console.log(date)
        const year = date.getFullYear()
        const month = String(date.getMonth() + 1).padStart(2, '0')
        const formattedDate = `${year}-${month}`
        this.dataForm.month = formattedDate
      }

      this.workManage({
        ...this.dataForm,
        page: this.page,
        pageSize: this.pageSize
      })
    },
    // 重置表单
    reset() {
      this.dataForm = {
        // 员工账号
        employeeNumber: '',
        // 员工姓名
        name: '',
        // 月份
        month: '',
        // 工作模式名称
        workPatternId: '',
        agencyId: null
      }
      this.workManage()
    },
    okbtn() {
      if (this.userIdList.length !== 0) {
        this.showDialog = true
      } else {
        this.$message.error('请选择调整人员')
      }
    },
    handleSelectionChange(selection) {
      console.log(selection)
      this.userIdList = []
      selection.forEach(item => this.userIdList.push(item.id))
      // console.log(this.userIdList)
      // console.log(111)
    },
    async radioinput(val) {
      console.log(val)
      this.workPatternType = val
      const res = await selectWorkHistoryList()
      console.log(res.data)
      this.workmodelList2 = res.data[val]
      // console.log(this.workmodelList)
    },
    workPatternChange(val) {
      console.log(val)
      this.workPatternId = val
    },
    async determine() {
      if (this.workPatternId) {
        await peopleSet({
          userIdList: this.userIdList,
          workPatternId: this.workPatternId,
          workPatternType: this.workPatternType || '1'
        })
        this.workManage()
        this.$message.success('修改成功')
        this.showDialog = false
        this.dataForm.workPatternId = ''
        this.radio = 1
        // console.log(res)
      } else {
        this.$message.error('请选择模式名称')
      }
    },
    cancle() {
      this.showDialog = false
      this.workPatternId2 = ''
      this.radio = 1
      this.radioinput(1)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep th.date {
  .cell {
    padding-left: 17px !important;
  }
}
.work {
  width: 24px;
  height: 24px;
  background: #ffeeeb;
  border: 1px solid #e15536;
  border-radius: 50%;
  text-align: center;
  color: #e15536;
  font-size: 13px;
}
.history-name {
  width: auto !important;
  ::v-deep .el-input__inner {
    width: 293px;
  }
}

.workArrange-manage-people-setting {
  ::v-deep .el-dialog__body {
    padding: 30px 50px 10px 50px !important;
  }
  ::v-deep .el-dialog__footer {
    padding-bottom: 40px;
  }
}

.work-type {
  ::v-deep .el-form-item__content {
    margin-left: 40px !important;
  }
  ::v-deep .el-radio:first-child {
    margin-right: 80px !important;
  }
  ::v-deep.el-radio__label {
    font-weight: bold;
    color: #333333;
  }
}

.dialog-footer {
  .el-button {
    width: 89px;
    height: 36px;
    padding: 10px 20px;
  }
}

.rest {
  width: 24px;
  height: 24px;
  background: #eff3f8;
  border: 1px solid #d8dde3;
  border-radius: 50%;
  text-align: center;
  color: #20232a;
  font-size: 13px;
}

.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
}
.transport-task ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

// 下拉框
::v-deep .el-select {
  width: 100%;
}

/*定义滚动条轨道 内阴影+圆角*/
::v-deep .el-table--scrollable-x .el-table__body-wrapper {
  padding-bottom: 5px;
  margin-bottom: 5px;
  &::-webkit-scrollbar {
    height: 10px;
  }

  /*定义滑块 内阴影+圆角*/
  &::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background-color: rgba(144, 147, 153, 0.3);
  }

  &::-webkit-scrollbar-track-piece {
    margin-right: 3px;
    margin-left: 3px;
  }
}
::v-deep .el-card {
  overflow: initial;
  margin: 20px;
}
</style>
